Imports System.Runtime.InteropServices
'Imports Microsoft.Office.Interop.Word
Imports Word
Imports System.IO


'****************************************
'Object handles printing of the barcode
'label from the barcodelabel.dot template
'from MS Word
'****************************************

Friend Class WordDoc

    Private Wrd As Application
    Private Doc As Document
    Private Rng As Range
    Private m_DS As DataSet
    Private TemplatePath As String = CurDir() & "\barcodelabeltemplate.dot"
    Private Img As String
    Private BatchNumImg As String
    Private TH As TextHandler

    Sub New()

        Wrd = New Application
        Doc = New Document
        m_DS = New DataSet
        TH = New TextHandler

    End Sub

    Friend Sub PrintBarcodeLabel(ByRef DS As DataSet, ByVal ImgPath As String, ByVal BatchNumImgPath As String)
        Try
            m_DS = DS
            Img = ImgPath
            BatchNumImg = BatchNumImgPath
            PrintLabel()
        Catch ex As COMException
            Throw New COMException("COM Exception:  " & ex.Message)
        Catch e As Exception
            Throw New Exception("Non-COM Exception:  " & e.Message)
        End Try
    End Sub

    Private Sub PrintLabel()

        Dim Dt As New Date
        Dim strTemp As String

        If TH Is Nothing Then
            TH = New TextHandler
        End If

        Dt = m_DS.Tables(0).Rows(0).Item("InDate").ToString

        Dim GradeNum As String
        Dim Grade As String

        If Not m_DS.Tables(0).Rows(0).Item("U_Grade") Is DBNull.Value Then
            GradeNum = m_DS.Tables(0).Rows(0).Item("U_Grade")
        Else
            GradeNum = Nothing
        End If

        Select Case GradeNum

            Case 1
                Grade = "First Quality"

            Case 2
                Grade = "Second Quality"

            Case 3
                Grade = "Damaged"

            Case 4
                Grade = "Second Greige Mill"

            Case Else
                Grade = "Quality Not Available"

        End Select

        Wrd.Visible = False
        Doc = Wrd.Documents.Open(TemplatePath)
        Doc.Activate()
        Wrd.Selection.Tables(1).Rows(2).Select()

        'Insert batch no.
        Doc.Tables(1).Rows(2).Cells(1).Select()
        Wrd.Selection.InlineShapes.AddPicture(BatchNumImg)
        'Wrd.Selection.TypeText(m_DS.Tables(0).Rows(0).Item("BatchNum").ToString)

        'Insert customer name and yards
        Wrd.Selection.MoveRight(, 2)
        'Dim TotalYards As Double = CDbl(m_DS.Tables(0).Rows(0).Item("U_TotalQty"))
        Dim TotalYards As Double = TH.RoundToOneDec(m_DS.Tables(0).Rows(0).Item("U_RecQty"))
        Wrd.Selection.TypeText(m_DS.Tables(0).Rows(0).Item("U_CustName").ToString & "           " & TotalYards.ToString & " Yards")

        'Insert first column of lot data
        Wrd.Selection.MoveRight(, 2)
        strTemp = "Date Packed:  " & Dt.ToShortDateString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Plant Lot No:  " & m_DS.Tables(0).Rows(0).Item("U_LotN0").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Cust Lot No:  " & m_DS.Tables(0).Rows(0).Item("U_CustLotNo").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Special Ord:  " & m_DS.Tables(0).Rows(0).Item("U_SpecialOrder").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Repack Ord:  " & m_DS.Tables(0).Rows(0).Item("U_RepackOrder").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Fabric Name:  " & TH.TrimStringLength(m_DS.Tables(0).Rows(0).Item("U_FabricName").ToString, 13) & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Fiber Code:  " & m_DS.Tables(0).Rows(0).Item("U_FiberCode").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Fiber Cont:  " & m_DS.Tables(0).Rows(0).Item("U_FiberCont").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))


        If m_DS.Tables(1).Rows.Count > 0 Then 'different table, must check for existing rows
            strTemp = "Markings:  " & m_DS.Tables(1).Rows(0).Item("U_Markings").ToString & Chr(11)
            Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))
        Else
            Wrd.Selection.TypeText("Markings:  ")
        End If

        'Insert second column of lot data
        Wrd.Selection.MoveRight(, 1)

        strTemp = "Grade:  " & Grade & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Prt Pattern:  " & m_DS.Tables(0).Rows(0).Item("U_PrintPat").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "C.C.:  " & m_DS.Tables(0).Rows(0).Item("U_CC").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Color Code:  " & m_DS.Tables(0).Rows(0).Item("U_ColorCode").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Shade:  " & m_DS.Tables(0).Rows(0).Item("U_ShadeSt").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        strTemp = "Fin Width:  " & m_DS.Tables(0).Rows(0).Item("U_FinWidth").ToString & Chr(11)
        Wrd.Selection.TypeText(TH.TrimStringLength(strTemp))

        Dim GrossWeight As Double = TH.RoundToOneDec(m_DS.Tables(0).Rows(0).Item("U_Grossw"))        
        Wrd.Selection.TypeText("Gross Wt:  " & TH.TrimStringLength(CStr(GrossWeight)) & Chr(11))

        Dim NetWeight As Double = TH.RoundToOneDec(m_DS.Tables(0).Rows(0).Item("U_Netweight"))
        Wrd.Selection.TypeText("Net Wt:  " & TH.TrimStringLength(CStr(NetWeight)) & Chr(11))

        'Insert barcode image
        Wrd.Selection.MoveRight(, 3)
        Wrd.Selection.InlineShapes.AddPicture(Img)

        'Print 2 labels per print command
        Dim i As Integer
        For i = 0 To 1
            Wrd.PrintOut()
        Next i

        'allow time for MS Word to process both print commands before shutting down
        System.Threading.Thread.Sleep(3000)
        Doc.Close(False, , )
        Wrd.Quit(False, , )

    End Sub

End Class
